/**
 * 现代化通知系统
 */
class NotificationSystem {
    constructor() {
        this.container = null;
        this.notifications = new Map();
        this.init();
    }
    
    init() {
        // 创建通知容器
        this.container = document.createElement('div');
        this.container.className = 'notification-container';
        document.body.appendChild(this.container);
    }
    
    /**
     * 显示通知
     * @param {string} message - 通知消息
     * @param {string} type - 通知类型 (success, error, warning, info)
     * @param {object} options - 配置选项
     */
    show(message, type = 'info', options = {}) {
        const config = {
            title: '',
            duration: 5000,
            closable: true,
            persistent: false,
            onClick: null,
            ...options
        };
        
        const notification = this.createNotification(message, type, config);
        const id = this.generateId();
        
        notification.setAttribute('data-id', id);
        this.notifications.set(id, notification);
        
        // 添加到容器
        this.container.appendChild(notification);
        
        // 触发显示动画
        requestAnimationFrame(() => {
            notification.classList.add('show');
        });
        
        // 自动关闭
        if (!config.persistent && config.duration > 0) {
            this.startAutoClose(id, config.duration);
        }
        
        return id;
    }
    
    /**
     * 创建通知元素
     */
    createNotification(message, type, config) {
        const notification = document.createElement('div');
        notification.className = `notification ${type}`;
        
        // 图标
        const icon = document.createElement('div');
        icon.className = 'notification-icon';
        
        // 内容
        const content = document.createElement('div');
        content.className = 'notification-content';
        
        if (config.title) {
            const title = document.createElement('div');
            title.className = 'notification-title';
            title.textContent = config.title;
            content.appendChild(title);
        }
        
        const messageEl = document.createElement('div');
        messageEl.className = 'notification-message';
        messageEl.textContent = message;
        content.appendChild(messageEl);
        
        notification.appendChild(icon);
        notification.appendChild(content);
        
        // 关闭按钮
        if (config.closable) {
            const closeBtn = document.createElement('button');
            closeBtn.className = 'notification-close';
            closeBtn.innerHTML = '×';
            closeBtn.onclick = () => this.close(notification.getAttribute('data-id'));
            notification.appendChild(closeBtn);
        }
        
        // 点击事件
        if (config.onClick) {
            notification.style.cursor = 'pointer';
            notification.onclick = config.onClick;
        }
        
        // 进度条
        if (!config.persistent && config.duration > 0) {
            const progress = document.createElement('div');
            progress.className = 'notification-progress';
            progress.style.width = '100%';
            notification.appendChild(progress);
        }
        
        return notification;
    }
    
    /**
     * 开始自动关闭倒计时
     */
    startAutoClose(id, duration) {
        const notification = this.notifications.get(id);
        if (!notification) return;
        
        const progress = notification.querySelector('.notification-progress');
        if (progress) {
            progress.style.transition = `width ${duration}ms linear`;
            progress.style.width = '0%';
        }
        
        setTimeout(() => {
            this.close(id);
        }, duration);
    }
    
    /**
     * 关闭通知
     */
    close(id) {
        const notification = this.notifications.get(id);
        if (!notification) return;
        
        notification.classList.remove('show');
        notification.classList.add('hide');
        
        setTimeout(() => {
            if (notification.parentNode) {
                notification.parentNode.removeChild(notification);
            }
            this.notifications.delete(id);
        }, 300);
    }
    
    /**
     * 关闭所有通知
     */
    closeAll() {
        this.notifications.forEach((notification, id) => {
            this.close(id);
        });
    }
    
    /**
     * 成功通知
     */
    success(message, options = {}) {
        return this.show(message, 'success', {
            title: '操作成功',
            ...options
        });
    }
    
    /**
     * 错误通知
     */
    error(message, options = {}) {
        return this.show(message, 'error', {
            title: '操作失败',
            duration: 8000,
            ...options
        });
    }
    
    /**
     * 警告通知
     */
    warning(message, options = {}) {
        return this.show(message, 'warning', {
            title: '注意',
            duration: 6000,
            ...options
        });
    }
    
    /**
     * 信息通知
     */
    info(message, options = {}) {
        return this.show(message, 'info', {
            title: '提示',
            ...options
        });
    }
    
    /**
     * 生成唯一ID
     */
    generateId() {
        return 'notification_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
    }
}

// 创建全局实例
window.Notifications = new NotificationSystem();

// 兼容旧的 showMessage 函数
window.showMessage = function(message, type = 'info', options = {}) {
    switch(type) {
        case 'success':
            return window.Notifications.success(message, options);
        case 'error':
            return window.Notifications.error(message, options);
        case 'warning':
            return window.Notifications.warning(message, options);
        default:
            return window.Notifications.info(message, options);
    }
};

// 快捷方法
window.showSuccess = (message, options) => window.Notifications.success(message, options);
window.showError = (message, options) => window.Notifications.error(message, options);
window.showWarning = (message, options) => window.Notifications.warning(message, options);
window.showInfo = (message, options) => window.Notifications.info(message, options);